//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zy.gpdemo.utils;

import com.yc.utils.common.Result;
import com.zy.gpdemo.domain.BasePrintTaskData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static com.yc.utils.common.IpUtils.localIp;

public class HttpApi {

    public HttpApi() {
    }

    public static Result post(String url, Map<String, Object> data) {
        Result result = null;

        try {
            if (data != null && data.size() > 0) {
                String param = "";

                String key;
                for (Iterator<String> var5 = data.keySet().iterator(); var5.hasNext(); param = param + key + "=" + data.get(key) + "&") {
                    key = var5.next();
                }

                url = url + "?" + param;
                url = url.substring(0, url.length() - 1);
            }

            URL URL = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) URL.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connection.setReadTimeout(3000);
            connection.setConnectTimeout(3000);
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            StringBuilder res = new StringBuilder();

            while ((line = br.readLine()) != null) {
                res.append(line).append("\n");
            }

            connection.disconnect();
            result = Result.ok(res.toString());
        } catch (SocketTimeoutException var8) {
            result = Result.error("失败！请求超时");
        } catch (FileNotFoundException var9) {
            result = Result.error("失败！接口路径" + url + "不存在");
        } catch (Exception var10) {
            result = Result.error(var10.getMessage());
        }

        return result;
    }

    public static List<BasePrintTaskData> getPrintTasks(String appRoot, String stockCode, String printerIp, String warehouseCode) {
        ArrayList<BasePrintTaskData> list = new ArrayList<>();

        try {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("stockCode", stockCode);
            paramMap.put("warehouseCode", warehouseCode);
            paramMap.put("printIp", printerIp);
            paramMap.put("billType", null);
            Result result = HttpApi.post(appRoot, paramMap);
            if (result.isFlag()) {
                String resultData = (String) result.getData();
                JSONObject jsonObject = JSONObject.fromObject(resultData);
                if(jsonObject.getString("data") != null && !"null".equals(jsonObject.getString("data"))) {
                	JSONArray array = JSONArray.fromObject(jsonObject.getString("data"));

                    for (Object object : array) {
                        BasePrintTaskData printTask = (BasePrintTaskData) JSONObject.toBean(JSONObject.fromObject(object), BasePrintTaskData.class);
                        list.add(printTask);
                    }
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    public static List<BasePrintTaskData> getPrintTasks() {
        ArrayList<BasePrintTaskData> list = new ArrayList<>();

        try {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("stockCode", "JQZZK");
            paramMap.put("warehouseCode", null);
            paramMap.put("printIp", localIp());
            paramMap.put("billType", null);
            Result result = HttpApi.post("http://192.168.26.2:8085/jac_parts_wms/basePrintServer", paramMap);
            if (result.isFlag()) {
                String resultData = (String) result.getData();
                JSONObject jsonObject = JSONObject.fromObject(resultData);
                if(jsonObject.getString("data") != null && !"null".equals(jsonObject.getString("data"))) {
                	JSONArray array = JSONArray.fromObject(jsonObject.getString("data"));

                    for (Object object : array) {
                        BasePrintTaskData printTask = (BasePrintTaskData) JSONObject.toBean(JSONObject.fromObject(object), BasePrintTaskData.class);
                        list.add(printTask);
                    }
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
